﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Text;

namespace WuxiWTO.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        WTODataContext ctx = new WTODataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            BuildTopNav();
            if (!IsPostBack)
            {
                SetTheLoginArea();
                BindLink();
            }
            GetSiteInfo();
            this.litBottomNav.Text = CategoryInfo.GetBottomNavi();
        }

        private void BindLink()
        {
            List<friend_link> links = ctx.friend_links.OrderBy(p => p.order_id).ToList();
            this.rptLinks.DataSource = links;
            this.rptLinks.DataBind();
        }

        private void GetSiteInfo()
        {
            site_setting setting = ctx.site_settings.SingleOrDefault<site_setting>(p => p.ID == 1);
            if (setting != null)
            {
                litIPC.Text = setting.site_ipc;
                Page.MetaKeywords = setting.site_keywords;
                Page.MetaDescription = setting.site_description;
                Page.Title = setting.site_name;
                litAddress.Text = setting.site_address;
                litCopyright.Text = setting.site_copyright;
                litTelephone.Text = setting.site_telephone;
            }
        }

        private void SetBottomLink()
        {
           
        }

        #region 页面顶部登录模块

        private void SetTheLoginArea()
        {
            //session不为空，已经登录
            if (Session["user_name"] != null)
            {
                string uname = Session["user_name"].ToString();
                var user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == uname);
                if (user != null)
                {
                    if (user.supporter_type == 'I')
                    {
                        loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.supporter_name);
                    }
                    else
                    {
                        loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.enterprise_name);
                    }
                }
                else
                {
                    loginStatus.Text = "<a class='top_a' href='login.aspx'>登录</a><a class='top_a' href='registerORG.aspx'>注册</a>";
                }
            }
            //session为空，查看cooki有没有
            else
            {
                if (Request.Cookies["wtouser"] == null)
                {
                    loginStatus.Text = "<a class='top_a' href='login.aspx'>登录</a><a class='top_a' href='registerORG.aspx'>注册</a>";
                }
                else
                {
                    string uname = Server.HtmlEncode(Request.Cookies["wtouser"]["name"].ToString());
                    var user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == uname);
                    if (user != null)
                    {
                        if (user.supporter_type == 'I')
                        {
                            loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.supporter_name);
                        }
                        else
                        {
                            loginStatus.Text = string.Format("欢迎您,<a href='userCenter.aspx'>{0}</a>&nbsp;&nbsp;<a class='top_a' href='logout.aspx'>退出</a>", user.enterprise_name);
                        }
                        Session["user_name"] = user.supporter_login_name;
                        Session["user_type"] = user.supporter_type;
                        Session["user_id"] = user.supporter_id;
                    }
                    else
                    {
                        loginStatus.Text = "<a class='top_a' href='login.aspx'>登录</a><a class='top_a' href='registerORG.aspx'>注册</a>";
                    }

                }
            }
        }
        #endregion

        private void BuildTopNav()
        {
            if (Cache["TopNav"] != null)
            {
                this.litTopNav.Text = Cache["TopNav"].ToString();
            }
            else
            {
                this.litTopNav.Text = CategoryInfo.GetTopNav();
                Cache["TopNav"] = this.litTopNav.Text;
            }
        }


        protected void rptLinks_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                friend_link link = e.Item.DataItem as friend_link;
                Image imgLink = e.Item.FindControl("imgLink") as Image;
                if (!string.IsNullOrEmpty(link.link_image))
                {
                    imgLink.ImageUrl = link.link_image;
                }
                else
                {
                    imgLink.ImageUrl = "~/images/link_noimage.gif";
                }
            }
        }
    }
}
